
#include <BLabel>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BColorDialog>
#include <BStyle>

#include "ImageLayer.h"
#include "CreateLayerDialog.h"

CreateLayerDialog::CreateLayerDialog(BWidget* parent) : BDialog(parent)
{
	this->setTitle("Create Layer");

	BLabel* nameLabel = new BLabel("name");
	_nameEdit = new BLineEdit();

	BLabel* formatLabel = new BLabel("Format : rgba");
	_formatComboBox = new BComboBox();
	_formatComboBox->addItem("RGBA", Format_RGBA);
	_formatComboBox->addItem("RGB", Format_RGB);
	_formatComboBox->addItem("Alpha", Format_Alpha);
	_formatComboBox->addItem("Gray", Format_Gray);
	_formatComboBox->addItem("Grapy_Alpha", Format_Gray_Alpha);
	_formatComboBox->setCurrentIndex(0);

	BLabel* widthLabel = new BLabel("Width");
	_widthSpinBox = new BSpinBox(1, 10000);
	_widthSpinBox->setSuffix("px");
	_widthSpinBox->setValue(100);

	BLabel* heightLabel = new BLabel("Height");
	_heightSpinBox = new BSpinBox(1, 10000);
	_heightSpinBox->setSuffix("px");
	_heightSpinBox->setValue(100);

	BLabel* colorLabel = new BLabel("color");
	_colorButton = new BButton(Graph_Square_Fill);
	_colorButton->setSizePolicy(Policy_Preferred);
	_colorButton->setColor(0, 0, 0);

	BGridLayout* gridLayout = new BGridLayout(this);
	gridLayout->addWidget(0, 0, nameLabel);
	gridLayout->addWidget(0, 1, _nameEdit);
	gridLayout->addWidget(1, 0, formatLabel);
	gridLayout->addWidget(1, 1, _formatComboBox);
	gridLayout->addWidget(2, 0, widthLabel);
	gridLayout->addWidget(2, 1, _widthSpinBox);
	gridLayout->addWidget(3, 0, heightLabel);
	gridLayout->addWidget(3, 1, _heightSpinBox);
	gridLayout->addWidget(4, 0, colorLabel);
	gridLayout->addWidget(4, 1, _colorButton, Align_LeftCenter);

	BHBoxLayout* bottomLayout = new BHBoxLayout();
	_createButton = new BButton("create");

	bottomLayout->addSpring();
	bottomLayout->addWidget(_createButton);
	this->setLayout(bottomLayout, Part_Bottom);

	connect(_nameEdit, Signal_TextEdited, &CreateLayerDialog::slotNameEdited);
	connect(_formatComboBox, Signal_ValueChanged, &CreateLayerDialog::slotFormatChanged);
	connect(_widthSpinBox, Signal_EditFinished, &CreateLayerDialog::slotSizeSpinBoxChanged);
	connect(_heightSpinBox, Signal_EditFinished, &CreateLayerDialog::slotSizeSpinBoxChanged);
	connect(_colorButton, Signal_Clicked, &CreateLayerDialog::slotChooseOriginColor);
	connect(_createButton, Signal_Clicked, &CreateLayerDialog::slotCreateButtonClicked);
}
CreateLayerDialog::~CreateLayerDialog()
{

}

void CreateLayerDialog::setNameList(const BStringList& nameList)
{
	_namelst = nameList;
}
const BStringList& CreateLayerDialog::nameList() const
{
	return _namelst;
}

void CreateLayerDialog::slotNameEdited(BObject* object, const BValue& value)
{
	const BString& text = value;
	BStyle* style = _nameEdit->style();
	if (!style)
	{
		style = new BStyle();
	}
	if (_namelst.contain(text))
	{
		style->setColor(Color_Text, BColor(200, 0, 0));
		_createButton->setEnabled(false);
	}
	else
	{
		style->removeColor(Color_Text);
		_createButton->setEnabled(true);
	}
	_nameEdit->setStyle(style);
}
void CreateLayerDialog::slotFormatChanged(BObject* object, const BValue& value)
{

}
void CreateLayerDialog::slotSizeSpinBoxChanged(BObject* object, const BValue& value)
{

}
void CreateLayerDialog::slotChooseOriginColor(BObject* object, const BValue& value)
{
	BColor color = BColorDialog::ChooseColor("Choose Origin Color", _colorButton->color());
	_colorButton->setColor(color);
}
void CreateLayerDialog::slotCreateButtonClicked(BObject* object, const BValue& value)
{
	Format format = _formatComboBox->value();
	int width = _widthSpinBox->value();
	int height = _heightSpinBox->value();
	BImage* image = new BImage(width, height, format);
	BColor color = _colorButton->color();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			image->setPixelColor(x, y, color);
		}
	}
	image->setName(_nameEdit->text());
	this->setAsset(image);
	this->close(true);
}

void CreateLayerDialog::showEvent(const BEvent& event)
{
	BDialog::showEvent(event);
	BString name("new-layer");
	int number = 0;
	while (_namelst.contain(name))
	{
		name.reset();
		name << "new-layer" << number++;
	}
	_nameEdit->setText(name);
}
